package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @program: duolaimall
 * @description: 登录、退出、登录状态验证
 * @author: zhong
 * @create: 2022-07-30 10:16
 **/

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    ILoginService loginService;
    @Autowired
    IKaptchaService kaptchaService;

    /**
     * 登录请求
     */
    @SneakyThrows //throw CopyBean Exception
    @PostMapping("/login")
    public ResponseData login(@RequestBody Map<String,String> loginFormBO, HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
        //验证码验证
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setCode(loginFormBO.get("captcha"));
        kaptchaCodeRequest.setUuid(CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid"));
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (kaptchaCodeResponse.getCode().equals(UserRetCode.KAPTCHA_CODE_ERROR.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        //用户名密码验证
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(loginFormBO.get("userName"));
        userLoginRequest.setPassword(loginFormBO.get("userPwd"));
        UserLoginResponse userLoginResponse = loginService.validateUser(userLoginRequest);
        if (userLoginResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            //将token存入cookie
            String token = userLoginResponse.getToken();
            Cookie cookie= CookieUtil.genCookie("access_token",token,"/",3600);
            cookie.setHttpOnly(true);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }
        return new ResponseUtil().setErrorMsg(userLoginResponse.getMsg());
    }

    @GetMapping("/login")
    public ResponseData getLoginState(HttpServletRequest httpServletRequest) {
        String token = CookieUtil.getCookieValue(httpServletRequest, "access_token");
        LoginStateResponse loginStateResponse = loginService.validateLogin(token);
        if (loginStateResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(loginStateResponse);
        }
        return new ResponseUtil().setErrorMsg(loginStateResponse.getMsg());
    }

    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
        String token = CookieUtil.getCookieValue(httpServletRequest, "access_token");
        LoginStateResponse loginStateResponse = loginService.validateLogin(token);
        if (loginStateResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            //清除cookie中的token
            Cookie cookie = CookieUtil.genCookie("access_token", null, "/", 3600);
            cookie.setHttpOnly(true);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil<>().setData(loginStateResponse);
        }
        return new ResponseUtil().setErrorMsg(loginStateResponse.getMsg());
    }
}
